import { Column, Entity, PrimaryGeneratedColumn, OneToMany, ManyToMany, JoinTable, OneToOne, AfterInsert, AfterRemove } from 'typeorm'
import { Logs } from '../logs/logs.entity'
import { Roles } from '../roles/roles.entity'
import { Profile } from './profile.entity'

@Entity()
export class User {
    [x: string]: any

    @PrimaryGeneratedColumn()
    id: number

    @Column({ unique: true })
    username: string

    @Column()
    password: string

    @OneToMany(() => Logs, (logs) => logs.user)
    logs: Logs[]

    @ManyToMany(() => Roles, (roles) => roles.users, { cascade: ['insert', 'remove'] })
    @JoinTable({ name: 'users_roles' })
    roles: Roles[]

    @OneToOne(() => Profile, (profile) => profile.user, { cascade: true })
    profile: Profile

    @AfterInsert()
    afterInsert() {
        console.log('User created', this.id, this.username,)
    }

    @AfterRemove()
    afterRemove() {
        console.log('User removed', this.id, this.username)
    }
}